<?php
/**
 * 内容展示页面
 */
namespace website\controllers;

use Yii;
use common\models\Category;
use common\models\User;
use common\models\Design;
use common\models\Recruit;
use common\models\RecruitApplicant;

class SeerController extends Controller {

    /**
     * 设计师简历浏览
     * @return mixed
     */
    public function actionResume()
    {
        if( ! ($user = User::canEdit($this->request->get('id')))) {
            return $this->error('请求异常，请稍后刷新页面重试', 'welcome/index');
        }
        if( ! ($user->designer() && $user->active())) {
            return $this->error('请求异常，请稍后刷新页面重试', 'welcome/index');
        }
        // 参数异常，渲染错误页面
        return $this->render('resume', [
            'user' => $user,
            'models' => Category::selector($this->params['designModelCategories']),
            'categories' => Category::selector($this->params['designCategories']),
        ]);
    }
    
    /**
     * 设计师简历浏览
     * @return mixed
     */
    public function actionDesign()
    {
        if( ! $design = Design::canEdit($this->request->get('id'))) {
            return $this->error('请求异常，请稍后刷新页面重试', 'welcome/index');
        }
        // 增加浏览次数
        $design->browser();
        // 参数异常，渲染错误页面
        return $this->render('design', [
            'design' => $design,
            'models' => Category::selector($this->params['designModelCategories']),
            'categories' => Category::selector($this->params['designCategories']),
        ]);
    }
    
    /**
     * 搜索招聘
     * @return mixed
     */
    public function actionRecruit()
    {
        if( ! ($this->isLogin() && $this->user->designer())) {
            return $this->error('访问异常，请稍后重试', 'welcome/index');
        }
        $condition['online'] = Recruit::OnlineOn;
        $condition['status'] = [Recruit::StatusRelease];
        if($this->user->inner()) {
            $condition['status'][] = Recruit::StatusExamine;
        }
        if( ! ($recruit = Recruit::canEdit($this->request->get('id'), $condition))) {
            return $this->error('招聘信息已下架，请选择其他招聘', 'search/recruit');
        }
        // 增加浏览次数
        $recruit->browser();
        return $this->render('recruit', [
            'recruit' => $recruit,
            'applicant' => RecruitApplicant::find()->where(['designer_id' => $this->user->id, 'status' => [RecruitApplicant::StatusExamine, RecruitApplicant::StatusAccept], 'recruit_id' => $recruit->id])->one(),
            'models' => Category::selector($this->params['designModelCategories']),
            'categories' => Category::selector($this->params['designCategories']),
        ]);
    }
}
